package com.example.zhapp_java.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhapp_java.R;
import com.example.zhapp_java.models.Project;

import java.util.List;
import java.util.function.Consumer;

public class ProjectAdapter extends RecyclerView.Adapter<ProjectAdapter.ViewHolder> {
    private final List<Project> projectList;
    private final Consumer<Project> onItemClick;

    public ProjectAdapter(List<Project> projectList, Consumer<Project> onItemClick) {
        this.projectList = projectList;
        this.onItemClick = onItemClick;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_project, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Project project = projectList.get(position);
        holder.tvProjectNo.setText(project.getProjectNo());
        holder.tvProjectDesc.setText(project.getProjectDesc());

        holder.itemView.setOnClickListener(v -> onItemClick.accept(project));
    }

    @Override
    public int getItemCount() {
        return projectList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvProjectNo, tvProjectDesc;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvProjectNo = itemView.findViewById(R.id.tvProjectNo);
            tvProjectDesc = itemView.findViewById(R.id.tvProjectDesc);
        }
    }
}